using System;
using System.Collections.Generic;
using FireStarter.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    /// <summary>
    /// Provides a place to write custom tests for UIGridFilter objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for UIGridFilter.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    public partial class TestUIGridFilter
    {
    [TestFixtureSetUp]
        public void SetupTestFixture()
        {
            //------------------------------------------------------------
            // Use this list to ignore generated tests that are failing
            // due to a unique condition in your application.
            // Remember to reimplement the test here.
            //------------------------------------------------------------
            //_ignoreList.Add("TestMethodName", "Reason for ignoring it");
            base.SetupFixture();
        }

        [Test]
        public void Test_ToString_WhenGridNull_ShouldReturnFormID()
        {
            //---------------Set up test pack-------------------
            UIGridFilter uiGridFilter = new UIGridFilter();
            //---------------Assert Precondition----------------
            Assert.IsNull(uiGridFilter.UIGridInfo);
            //---------------Execute Test ----------------------
            string actualToString = uiGridFilter.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(uiGridFilter.UIGridFilterID.ToString(), actualToString);
        }
        [Test]
        public void Test_ToString_WhenGridNotNull_ShouldReturnGridToString_GridFilter()
        {
            //---------------Set up test pack-------------------
            UIGridFilter uiGridFilter = TestUtilsUIGridFilter.CreateUnsavedValidUIGridFilter();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(uiGridFilter.UIGridInfo);
            //---------------Execute Test ----------------------
            string actualToString = uiGridFilter.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(uiGridFilter.UIGridInfo + "_GridFilter", actualToString);
        }

        [Test]
        public void Test_IFilterPropertyDef_Label_Get()
        {
            //---------------Set up test pack-------------------
            UIGridFilter gridFilterInfo = TestUtilsShared.CreateUnsavedUIGridFilterInfo();
            UIGridFilterProp gridFilterProp = gridFilterInfo.UIGridFilterProps[0];
            IFilterPropertyDef filterPropertyDef = gridFilterProp;
            //---------------Assert Precondition----------------
            Assert.Greater(gridFilterProp.LabelText.Length, 0);
            //---------------Execute Test ----------------------
            string label = filterPropertyDef.Label;
            //---------------Test Result -----------------------
            Assert.AreEqual(gridFilterProp.LabelText, label);
        }

        [Test]
        public void Test_IFilterPropertyDef_FilterClauseOperator_Get()
        {
            //---------------Set up test pack-------------------
            UIGridFilter gridFilterInfo = TestUtilsShared.CreateUnsavedUIGridFilterInfo();
            UIGridFilterProp gridFilterProp = gridFilterInfo.UIGridFilterProps[0];
            IFilterPropertyDef filterPropertyDef = gridFilterProp;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(gridFilterProp.FilterClauseOp);
            //---------------Execute Test ----------------------
            FilterClauseOperator filterClauseOperator = filterPropertyDef.FilterClauseOperator;
            //---------------Test Result -----------------------
            Assert.AreEqual(filterClauseOperator, filterClauseOperator);
        }

        [Test]
        public void Test_IFilterPropertyDef_FilterClauseOperator_Set()
        {
            //---------------Set up test pack-------------------
            UIGridFilter gridFilterInfo = TestUtilsShared.CreateUnsavedUIGridFilterInfo();
            IFilterPropertyDef gridFilterProp = gridFilterInfo.UIGridFilterProps[0];

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            gridFilterProp.FilterClauseOperator = FilterClauseOperator.OpLessThanOrEqualTo;

            //---------------Test Result -----------------------
            Assert.AreEqual(FilterClauseOperator.OpLessThanOrEqualTo, gridFilterProp.FilterClauseOperator);
        }


        [Test]
        public void Test_IFilterPropertyDef_FilterType_Get()
        {
            //---------------Set up test pack-------------------
            string assemblyName = BOBroker.GetMapperAssemblyName();
            const string controlType = "StringTextBoxFilter";
            UIGridFilter gridFilterInfo = TestUtilsShared.CreateUnsavedUIGridFilterInfo();
            UIGridFilterProp gridFilterProp = gridFilterInfo.UIGridFilterProps[0];
            gridFilterProp.UIGridFilterType = GetUIFilterControlType(gridFilterInfo, assemblyName, controlType);
            IFilterPropertyDef filterPropertyDef = gridFilterProp;

            //---------------Assert Precondition----------------
            Assert.IsNotNull(gridFilterProp.UIGridFilterType);
            //---------------Execute Test ----------------------
            string filterType = filterPropertyDef.FilterType;
            //---------------Test Result -----------------------
            Assert.AreEqual(gridFilterProp.UIGridFilterType.TypeName, filterType);
        }

        private UITypeDefinition GetUIFilterControlType(UIGridFilter gridFilterInfo, string assemblyName, string controlType)
        {
            return ((DMSolution)gridFilterInfo.UIGridInfo.UIView.Class.Solution).GetUIFilterControlType(
                assemblyName, controlType);
        }

        [Test]
        public void Test_IFilterPropertyDef_FilterType_Get_NoFilterControlTypeSet()
        {
            //---------------Set up test pack-------------------
            UIGridFilterProp gridFilterProp = new UIGridFilterProp();
            IFilterPropertyDef filterPropertyDef = gridFilterProp;

            //---------------Assert Precondition----------------
            Assert.IsNull(gridFilterProp.UIGridFilterType);
            //---------------Execute Test ----------------------
            string filterType = filterPropertyDef.FilterType;
            //---------------Test Result -----------------------
            Assert.AreEqual("", filterType);
        }



        [Test]
        public void Test_IFilterPropertyDef_FilterType_Set()
        {
            //---------------Set up test pack-------------------
            IFilterPropertyDef filterPropertyDef = new UIGridFilterProp();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            try
            {
                filterPropertyDef.FilterType = TestUtilsShared.GetRandomString();
                Assert.Fail("Expected to throw an NotImplementedException");
            }
            //---------------Test Result -----------------------
            catch (NotImplementedException ex)
            {
                StringAssert.Contains("This should be set when the UIGridFilterType is set for the UIGridFilterProp.", ex.Message);
            }
        }

        [Test]
        public void Test_IFilterPropertyDef_FilterTypeAssembly_Get()
        {
            //---------------Set up test pack-------------------
            string assemblyName = BOBroker.GetMapperAssemblyName();
            const string controlType = "StringTextBoxFilter";
            UIGridFilter gridFilterInfo = TestUtilsShared.CreateUnsavedUIGridFilterInfo();
            UIGridFilterProp gridFilterProp = gridFilterInfo.UIGridFilterProps[0];
            var dmSolution = (DMSolution)gridFilterInfo.UIGridInfo.UIView.Class.Solution;
            gridFilterProp.UIGridFilterType = dmSolution.GetUIFilterControlType(
                        assemblyName, controlType);
            IFilterPropertyDef filterPropertyDef = gridFilterProp;

            //---------------Assert Precondition----------------
            Assert.IsNotNull(gridFilterProp.UIGridFilterType);
            //---------------Execute Test ----------------------
            string filterTypeAssembly = filterPropertyDef.FilterTypeAssembly;
            //---------------Test Result -----------------------
            Assert.AreEqual(gridFilterProp.UIGridFilterType.AssemblyName, filterTypeAssembly);
        }

        [Test]
        public void Test_IFilterPropertyDef_FilterTypeAssembly_Get_NoFilterControlTypeSet()
        {
            //---------------Set up test pack-------------------
            UIGridFilterProp gridFilterProp = new UIGridFilterProp();
            IFilterPropertyDef filterPropertyDef = gridFilterProp;

            //---------------Assert Precondition----------------
            Assert.IsNull(gridFilterProp.UIGridFilterType);
            //---------------Execute Test ----------------------
            string filterTypeAssembly = filterPropertyDef.FilterTypeAssembly;
            //---------------Test Result -----------------------
            Assert.AreEqual("", filterTypeAssembly);
        }

        [Test]
        public void Test_IFilterPropertyDef_FilterTypeAssembly_Set()
        {
            //---------------Set up test pack-------------------
            IFilterPropertyDef filterPropertyDef = new UIGridFilterProp();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            try
            {
                filterPropertyDef.FilterTypeAssembly = TestUtilsShared.GetRandomString();
                Assert.Fail("Expected to throw an NotImplementedException");
            }
            //---------------Test Result -----------------------
            catch (NotImplementedException ex)
            {
                StringAssert.Contains("This should be set when the UIGridFilterType is set for the UIGridFilterProp.", ex.Message);
            }
        }

        [Test]
        public void Test_IFilterPropertyDef_Parameters_Get_NoParameters()
        {
            //---------------Set up test pack-------------------
            UIGridFilterProp gridFilterProp = new UIGridFilterProp();
            IFilterPropertyDef filterPropertyDef = gridFilterProp;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            Dictionary<string, string> returnedParameters = filterPropertyDef.Parameters;

            //---------------Test Result -----------------------
            BusinessObjectCollection<UIParameter> uiParameters = gridFilterProp.UIParameters;
            Assert.AreEqual(uiParameters.Count, returnedParameters.Count);
            foreach (UIParameter parameter in uiParameters)
            {
                Assert.IsTrue(returnedParameters.ContainsKey(parameter.Name));
                Assert.IsTrue(returnedParameters.ContainsValue(parameter.Value));
            }
        }

        [Test]
        public void Test_IFilterPropertyDef_Parameters_Get_WithParameters()
        {
            //---------------Set up test pack-------------------
            UIGridFilter gridFilterInfo = TestUtilsShared.CreateUnsavedUIGridFilterInfo();
            UIGridFilterProp gridFilterProp = gridFilterInfo.UIGridFilterProps[0];
            UIParameter uiParameter = gridFilterProp.UIParameters.CreateBusinessObject();
            TestUtilsShared.UpdateUIParameterInfoWithValidData(uiParameter);
            IFilterPropertyDef filterPropertyDef = gridFilterProp;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, gridFilterProp.UIParameters.Count);
            //---------------Execute Test ----------------------
            Dictionary<string, string> returnedParameters = filterPropertyDef.Parameters;

            //---------------Test Result -----------------------
            BusinessObjectCollection<UIParameter> uiParameters = gridFilterProp.UIParameters;
            Assert.AreEqual(uiParameters.Count, returnedParameters.Count);
            foreach (UIParameter parameter in uiParameters)
            {
                Assert.IsTrue(returnedParameters.ContainsKey(parameter.Name));
                Assert.IsTrue(returnedParameters.ContainsValue(parameter.Value));
            }
        }

        [Test]
        public void Test_IFilterPropertyDef_Parameters_Set()
        {
            //---------------Set up test pack-------------------
            UIGridFilterProp gridFilterProp = new UIGridFilterProp();
            IFilterPropertyDef filterPropertyDef = gridFilterProp;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            filterPropertyDef.Parameters = new Dictionary<string, string>();
            //---------------Test Result -----------------------
            Assert.AreEqual(0, filterPropertyDef.Parameters.Count);
        }

        [Test]
        public void Test_GetParent()
        {
            //---------------Set up test pack-------------------
            UIGridFilter uiGridFilter = new UIGridFilter();
            UIGridFilterProp uiGridFilterProp = new UIGridFilterProp();
            uiGridFilter.UIGridFilterProps.Add(uiGridFilterProp);
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IModelElement parent = ((IModelElement)uiGridFilterProp).Parent;
            //---------------Test Result -----------------------
            Assert.AreSame(uiGridFilter, parent);
        }

        [Test]
        public void Test_SetParentExceptionThrown()
        {
            //---------------Set up test pack-------------------
            IModelElement prop = new UIGridFilterProp();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            Exception exceptionThrown = null;
            try
            {
                prop.Parent = new UIGridFilter();
            }
            catch (Exception ex)
            {
                exceptionThrown = ex;
            }
            //---------------Test Result -----------------------
            Assert.IsNotNull(exceptionThrown);
            Assert.IsInstanceOf(typeof(HabaneroDeveloperException), exceptionThrown);
            StringAssert.Contains
                ("Set the parent of the UIGridFilterProp by setting the related UIGridFilter object",
                 exceptionThrown.Message);
        }
    
    }
}